<template>
    <el-dialog :title="title" :visible="visible" width="40%" :show-close="true" :close-on-click-modal="false">
        <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="120px" class="demo-ruleForm">
            <el-form-item label="优惠券名称" prop='name'>
                <el-input v-model="ruleForm.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="优惠券类型" prop="type">
                <el-select v-model="ruleForm.type" placeholder="请选择优惠券类型" style="width: 100%;">
                    <el-option label="折扣券" :value="1" />
                    <el-option label="满减券" :value="2" />
                </el-select>
            </el-form-item>
            <el-form-item label="商品分类" prop="category_ids">
                <el-select v-model="ruleForm.category_ids" multiple placeholder="请选择商品分类" style="width: 100%;">
                    <el-option :label="i.label" :value="i.value" v-for="i in goods_category" />
                </el-select>
            </el-form-item>
            <el-form-item label="优惠券面值" prop="amount">
                <el-input type="number" v-model="ruleForm.amount" placeholder="请输入优惠券面值" />
            </el-form-item>
            <el-form-item label="最低消费金额" prop="min_amount">
                <el-input type="number" v-model="ruleForm.min_amount" placeholder="请输入最低消费金额" />
            </el-form-item>
            <el-form-item label="发行总量" prop="total">
                <el-input type="number" v-model="ruleForm.total" placeholder="请输入发行总量" />
            </el-form-item>
            <el-form-item label="每人限领数量" prop="per_limit">
                <el-input type="number" v-model="ruleForm.per_limit" placeholder="请输入每人限领数量" />
            </el-form-item>
            <el-form-item label="限时时间" prop="timeRange">
                <el-date-picker v-model="ruleForm.timeRange" type="datetimerange" range-separator="至"
                    start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="ruleForm.status" placeholder="请选择">
                    <el-option label="启用" :value="1" />
                    <el-option label="禁用" :value="0" />
                </el-select>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input type="number" v-model="ruleForm.sort" placeholder="请输入地址" />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="submit" :loading="loading">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { couponAdd, couponEdit } from '../api/index';
import Upload from '@/components/Upload.vue';
export default {
    props: ['merchant_id', 'goods_category'],
    data() {
        return {
            isEdit: false,
            options: [],
            title: '',
            visible: false,
            loading: false,
            ruleForm: {
                name: null,
                type: null,
                amount: null,
                min_amount: null,
                total: null,
                per_limit: null,
                status: null,
                sort: null,
                timeRange: [],
                category_ids: [],
            },
            rules: {
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' }
                ],
                type: [
                    { required: true, message: '请选择优惠券类型', trigger: 'blur' }
                ],
                amount: [
                    { required: true, message: '请输入优惠券面值', trigger: 'blur' }
                ],
                min_amount: [
                    { required: true, message: '请输入最低消费金额', trigger: 'blur' }
                ],
                total: [
                    { required: true, message: '请输入发行总量', trigger: 'blur' }
                ],
                per_limit: [
                    { required: true, message: '请输入每人限领数量', trigger: 'blur' }
                ],
                status: [
                    { required: true, message: '请选择优惠券状态', trigger: 'blur' }
                ],
                // sort: [
                //     { required: true, message: '请输入分类排序', trigger: 'blur' }
                // ],
                timeRange: [
                    { required: true, message: '请选择优惠券有效期', trigger: 'blur' }
                ],
                category_ids: [
                    { required: true, message: '请选择商品分类', trigger: 'blur' }
                ],
            },
        }
    },
    components: { Upload },
    created() {

    },
    methods: {
        add() {
            this.visible = true;
            this.isEdit = false;
            this.title = '新增优惠券';
            let temp = {};
            Object.keys(this.ruleForm).forEach(key => {
                temp[key] = null
            })
            this.ruleForm = temp;
        },
        edit(data) {
            console.log(data);

            this.title = '修改优惠券';
            this.visible = true;
            this.isEdit = true;
            this.editId = data.id;
            Object.keys(this.ruleForm).forEach(key => {
                this.ruleForm[key] = data[key]
            })
            this.ruleForm.timeRange = [data.start_time, data.end_time];
        },
        close() {
            this.$refs.formRef.resetFields();
            this.visible = false;
        },
        submit() {
            this.$refs.formRef.validate(async (res) => {
                if (res) {
                    this.loading = true;
                    try {
                        const p = {
                            ...this.ruleForm,
                            merchant_id: this.merchant_id,
                            start_time: this.ruleForm.timeRange ? this.ruleForm.timeRange[0] : null,
                            end_time: this.ruleForm.timeRange ? this.ruleForm.timeRange[1] : null,
                        }
                        console.log(p);

                        let res = this.isEdit ? await couponEdit({ ...p, id: this.editId }) : await couponAdd(p);
                        this.loading = false;
                        this.$refs.formRef.resetFields();
                        const msg = this.title + '成功';
                        this.$message({
                            type: 'success',
                            message: msg
                        })
                        this.close();
                        this.$emit('refresh', 1);
                    } catch (err) {
                        console.log(err);

                        this.loading = false;
                    }
                }
            })
        },
    },
}
</script>

<style lang='scss' scoped>
.avatar-uploader .el-upload {
    border: 1px dashed red;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>